<?php
/**
 * Class with collection of useful functions
 * 
 * @author akovalyov@gmail.com
 *
 */
class Functions {
	
	/**
	 * 
	 * @param array '{code}'=>'1234'
	 * @param string
	 */
	public static function str_replace_assoc($array,$string){
	    $from_array = array();
	    $to_array = array();
	   
	   foreach ($array as $k => $v){
	        $from_array[] = $k;
	        $to_array[] = $v;
	   }
        return str_replace($from_array,$to_array,$string);
    }
    
	/**
	 * convert date
	 * @param $str date
	 * @param $sFormat defualt Y-m-d
	 */
	public static function convertDateFormat($str, $sFormat = 'Y-m-d') {
		$date = date($sFormat, strtotime($str));
		return $date;
	}
	
	/**
	 * Convert to sql timestamp 'Y-m-d H:i:s'
	 * @param int $timestamp default = time();
	 * @return string
	 */
	public static function converToSqlTimestamp($timestamp = null)
	{
		if(!$timestamp)
			$timestamp = time();
			
		return date('Y-m-d H:i:s', $timestamp);
	}
	
	
	/**
	 * translation from the cyrillic to the transcript
	 */ 
	public static function encodestring($str) {
		$translit = array(
            "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
            "Д"=>"D","Е"=>"E","Ж"=>"J","З"=>"Z","И"=>"I",
            "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
            "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
            "У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
            "Ш"=>"SH","Щ"=>"SCH","Ъ"=>"","Ы"=>"YI","Ь"=>"",
            "Э"=>"E","Ю"=>"YU","Я"=>"YA","а"=>"a","б"=>"b",
            "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
            "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
            "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
            "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
            "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
            "ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya"
        );
        
        return strtr($str,$translit);
	}
	
}